﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Gnosis.Alexandria;
using Gnosis.Alexandria.Model.Commands;
using Gnosis.Alexandria.Model.Repositories;
using Gnosis.Alexandria.Data;
using Gnosis.Alexandria.Model;

namespace Gnosis.Alexandria.Controllers
{
    public class RepositoryController
        : ControllerOldBase
    {
        public RepositoryController(MainController parent)
            : base(parent)
        {
        }

        private readonly IRepository _repository = new CatalogRepository();

        private static IList<SearchDetailData> GetSearchDetailData(IEnumerable<IArtist> artists)
        {
            var results = new List<SearchDetailData>();
            foreach (IArtist item in artists)
            {
                var data = new SearchDetailData
                {
                    Id = item.Id,
                    Name = item.Name,
                    From = item.Country.Name,
                    Date = item.StartDate,
                    Note = item.Note
                };

                results.Add(data);
            }

            return results;
        }

        protected override void HandleMessage(IMessageOld message)
        {
            switch (message.Type)
            {
                case MessageTypes.RequestControllerInitialize:
                    _repository.Initialize();
                    break;
                case MessageTypes.RequestNavigateViewSearch:
                {
                    var requestData = (SearchRequestData)message.Body;
                    var query = new SelectByNameOrHash( requestData.Search);
                    var result = _repository.Get<IArtist>(query);
                    var resultData = new SearchResultData { Search = requestData.Search, Source = requestData.Source, Result = GetSearchDetailData(result) };
                    Send(this, MessageTypes.ResponseNavigateViewSearch, resultData);
                    break;
                }
                case MessageTypes.RequestRepositoryGetArtist:
                {
                    var countries = _repository.GetAll<ICountry>();
                    IList<CountryData> countryData = new List<CountryData>();
                    foreach (ICountry country in countries)
                    {
                        countryData.Add(new CountryData
                        {
                            Id = country.Id,
                            Name = country.Name,
                            Hash = country.Hash,
                            Code = country.Code
                        });
                    }

                    var requestData = message.Body as ArtistEditData;
                    var artist = _repository.Get<IArtist>(requestData.Id);
                    if (artist != null && artist.Id == requestData.Id)
                    {
                        var responseData = new ArtistEditData
                        {
                            Id = requestData.Id,
                            Name = artist.Name,
                            Abbreviation = artist.Abbreviation,
                            Note = artist.Note,
                            CountryId = artist.Country.Id,
                            StartDate = artist.StartDate,
                            EndDate = artist.EndDate,
                            Countries = countryData
                        };
                        Send(this, MessageTypes.ResponseRepositoryGetArtist, responseData);
                    }
                    break;
                }
                case MessageTypes.RequestArtistEditSave:
                {
                    var data = message.Body as ArtistSaveData;
                    var artist = _repository.Get<IArtist>(data.Id);
                    artist.Name = data.Name;
                    artist.Abbreviation = data.Abbreviation;
                    artist.Note = data.Note;
                    artist.StartDate = data.StartDate;
                    artist.EndDate = data.EndDate;
                    _repository.Persist<IArtist>(artist);
                    Send(this, MessageTypes.ResponseArtistSaveSucceeded);
                    break;
                }
                default:
                    break;
            }
        }
    }
}
